<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------

namespace app\common\model;
use think\Validate;
use think\model\concern\SoftDelete;

class StudentExceptInfo extends Common
{
    /**
     * @param $post
     *
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function tableData($post)
    {
        if(isset($post['limit'])){
            $limit = $post['limit'];
        }else{
            $limit = config('paginate.list_rows');
        }

        $tableWhere = $this->tableWhere($post);
        $list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
        $data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型

        $re['code'] = 0;
        $re['msg'] = '';
        $re['count'] = $list->total();
        $re['data'] = $data;

        return $re;
    }

    /**
     *  添加异常学生数据
     * User:tony
     * @param $data
     * @return array
     */
    public function addData($data)
    {
        $result = [
            'status' => true,
            'msg' => '锁定成功',
            'data'=> [],
            'token'  => \think\facade\Request::token('__ZlServer_Token__', 'sha1')
        ];

        $info = $this->where(['xh'=>$data['xh'], 'teacher'=>$data['teacher'], 'year'=>$data['year'], 'season'=>$data['season']])->find();
        if($info){
            $result['status'] = false;
            $result['msg'] = '该学生已经锁定！';   
        } else {
            if (!$this->allowField(true)->save($data))
            {
                $result['status'] = false;
                $result['msg'] = '保存失败';
            }
        }

        return $result;
    }
  
    /**
     *  添加异常学生数据
     * User:tony
     * @param $data
     * @return array
     */
    public function del($data)
    {
        $result = [
            'status' => true,
            'msg' => '解锁成功',
            'data'=> [],
            'token'  => \think\facade\Request::token('__ZlServer_Token__', 'sha1')
        ];
          
        $info = $this->where(['xh'=>$data['xh'], 'year'=>$data['year'], 'season'=>$data['season']])->find();
        if($info){
            $this->where(['xh'=>$data['xh'], 'year'=>$data['year'], 'season'=>$data['season']])->delete();
        } else { 
            $result['status'] = false;
            $result['msg'] = '该学生不存在锁定记录'; 
        }

        return $result;
    }
 
    //where搜索条件
    protected function tableWhere($post)
    {
        $where = [];

        $result['where'] = $where;
        $result['field'] = "*";
        $result['order'] = ['id ASC'];
        return $result;
    }
 
    /**
     * @param $list
     *
     * @return mixed
     */
    protected function tableFormat($list)
    {
        return $list;
    }


}